package com.controller.lib.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.RequestOptions;
import com.controller.lib.R;
import com.google.android.exoplayer2.ui.PlayerView;
import timber.log.Timber;

import static com.google.android.exoplayer2.ui.AspectRatioFrameLayout.RESIZE_MODE_FILL;

/**
 * @author : GeRui
 * date   : 2025/6/25 13:32
 * desc   :加载系统壁纸
 * version: 1.0
 */
public class WallpaperImageActivity extends AppCompatActivity {
  private ImageView ivWallpaper;

  @Override
  protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    LinearLayout rootLayout = new LinearLayout(this);
    rootLayout.setOrientation(LinearLayout.VERTICAL);
    rootLayout.setBackgroundColor(getResources().getColor(android.R.color.transparent));
    rootLayout.setLayoutParams(new LinearLayout.LayoutParams(
        ViewGroup.LayoutParams.MATCH_PARENT,
        ViewGroup.LayoutParams.MATCH_PARENT
    ));
    ivWallpaper=new ImageView(this);
    FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
        ViewGroup.LayoutParams.MATCH_PARENT,
        ViewGroup.LayoutParams.MATCH_PARENT
    );
    params.gravity = Gravity.CENTER;
    ivWallpaper.setLayoutParams(params);
    rootLayout.addView(ivWallpaper);
    setContentView(rootLayout);
    loadWallpaperImage(getIntent());
  }

  @Override protected void onStop() {
    super.onStop();
    Timber.d("WallpaperImageActivity onStop");
  }

  @Override protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    loadWallpaperImage(intent);
  }

  /**
   * 加载图片
   */
  private void loadWallpaperImage(Intent intent) {
    if (intent != null) {
      String imageUrl = intent.getStringExtra("image_url");
      if (!TextUtils.isEmpty(imageUrl)) {
        Glide.with(this)
            .applyDefaultRequestOptions(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.ALL))
            .load(imageUrl)
            // 淡入淡出效果
            .transition(DrawableTransitionOptions.withCrossFade())
            .into(ivWallpaper);
      }
    }
  }
}